<template>
  <el-container class="index">
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="title">
        <h1>电商后台管理系统</h1>
      </div>
      <div class="logout">欢迎光临~ <a href="javascript:;" @click="logout">退出</a></div>
    </el-header>
    <el-container>
      <el-aside width="200px">
         <el-menu
          router
          unique-opened
          :default-active="defaultActive"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b">
          <el-submenu :index="l1.path" v-for="l1 in menuList" :key="l1.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ l1.authName }}</span>
            </template>
            <el-menu-item :index="item.path" v-for="item in l1.children" :key="item.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{ item.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: []
    }
  },
  async created () {
    // console.log(222)
    const { meta, data } = await this.$axios.get('menus')
    if (meta.status === 200) {
      this.menuList = data
      // console.log(data)
    }
  },
  methods: {
    async logout () {
      try {
        await this.$confirm('亲, 你确认要退出系统吗?', '温馨提示', { type: 'warning' })
        localStorage.removeItem('token')
        this.$router.push('/login')
      } catch (err) {
        console.log(err)
      }
    }
  },
  computed: {
    defaultActive () {
      // console.log(this.$route.path.slice(1))
      return this.$route.path.slice(1).split('-')[0]
    }
  }
}
</script>

<style lang="scss" scoped>
.index{
  height: 100%;
  .el-header{
    background-color: #d8d8d8;
    padding: 0;
    display: flex;
    text-align: center;
    .logo{
      width: 200px;
      img{
      height: 40px;
      margin-top: 10px;
      margin-left: 20px;
      }
    }
    .title{
      flex: 1;
      color: #545c64;
      line-height: 60px;
    }
    .logout{
      width: 200px;
      text-align: right;
      line-height: 60px;
      padding-right: 10px;
      font-weight: 700;
      a{
        color: orange;
      }
    }
  }
  .el-container{
    .el-aside{
      background-color: #545c64;
      .el-menu{
        border-right: none;
      }
    }
    .el-main{
      background-color: #ecf0f1;
    }
  }
}

</style>
